<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        ul {
            width: 600px;
            margin: 50px auto;
            list-style: none;
        }

        li {
            height: 50px;
        }
    </style>
</head>

<body>
    <input id="btn" type="button" value="新增">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</body>
<script>
    // 获取元素 注册事件
    var ul = document.querySelector('ul');
    var btn = document.querySelector('#btn');
    // var li = document.querySelectorAll('li')
    btn.onclick = function(){
        let li = document.createElement('li');
        li.innerText = Math.floor(Math.random()*10);
        ul.insertBefore(li,ul.children[0]);
    }
    ul.onclick = function (e) {
        // console.log(e.target.nodeName);
        if(e.target.nodeName === 'LI'){
            e.target.style.background = 'skyblue';
        }
    }

</script>

</html>